package com.sxz.sbs.module.client;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.presenter.AddUserAccountPresenter;
import com.sxz.sbs.module.client.view.AddUserAccountView;
import com.sxz.sbs.util.IsPhoneFormatUtils;
import com.sxz.sbs.util.VerifyCountDownUtil;
import com.sxz.sbs.widget.CommonTitle;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加用户账号
 *
 * @author hp
 * @date 2018/3/21
 */
public class AddUserAccountActivity extends BaseActivity<AddUserAccountPresenter> implements AddUserAccountView {

    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.btn_add_useraccount)
    Button btnAddUserAccount;
    @BindView(R.id.btn_sendverification)
    Button btnSendverification;
    private String phone = "";
    private String code = "";
    private VerifyCountDownUtil countDownUtil;

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("添加用户账号").build().show();
        edPhone.addTextChangedListener(mTextWatcher);
        edCode.addTextChangedListener(mTextWatcher);
    }

    @OnClick({R.id.btn_sendverification, R.id.btn_add_useraccount})
    public void addUserAccountOnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sendverification:
                if (phone.length() == 11 && RegexUtils.isMobileExact(phone)) {
                    if (phone.isEmpty()) {
                        ToastUtils.showShort("请输入手机号");
                    } else {
                        HashMap<String, Object> map = HttpParamMap.getParamMap();
                        map.put("flag", "2");
                        map.put("phone", phone);
                        mPresenter.getVerifyCode(map);
                    }
                } else {
                    ToastUtils.showShort("请输入正确的手机号");
                }
                break;
            default:
                break;
        }
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            phone = edPhone.getText().toString();
            code = edCode.getText().toString();
            if (phone.length() == 11 && RegexUtils.isMobileExact(phone) && code.length() == 6) {
                btnAddUserAccount.setBackgroundResource(R.drawable.click_bg1);
                btnAddUserAccount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        HashMap<String, Object> map1 = HttpParamMap.getParamMap();
                        map1.put("phone", phone);
                        map1.put("code", code);
                        mPresenter.getAddUserAccount(map1);
                    }
                });
            } else {
                btnAddUserAccount.setBackgroundResource(R.drawable.click_bg2);
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_user_account;
    }

    @Override
    protected AddUserAccountPresenter createPresenter() {
        return new AddUserAccountPresenter();
    }

    @Override
    public void onGetCodeSuccess() {
        Log.e("TAAAA", "AAAAAAAA");
        countDownUtil = new VerifyCountDownUtil(btnSendverification);
        countDownUtil.start();
    }

    @Override
    public void onGetAddUserAccount() {
        Intent intent = new Intent(AddUserAccountActivity.this, AddUserSuccessActivity.class);
        intent.putExtra("phone", phone);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownUtil != null) {
            countDownUtil.destroy();
        }
    }
}
